package org.cycuchy.hp.components.book.kapstelki {
	
	import com.rubenswieringa.book.Page;
	
	import flash.display.DisplayObject;
	
	import mx.collections.ArrayCollection;
	
	import org.cycuchy.hp.components.GradientCanvas;
	import org.cycuchy.hp.components.KapstelekImageCanvas;
	import org.cycuchy.hp.components.book.NumeredPage;
	import org.cycuchy.hp.data.KapstelekData;
	import org.cycuchy.hp.data.KapstelkiData;
	import org.cycuchy.hp.event.PreloadingImageEvent;

	public class KapstelikiPage__ extends NumeredPage {
		
//        [Embed(source='assets/img/11.jpg')]
//        private static var backgroundImage:Class;		
		
		private var loaded:Boolean = false;
		
		private var _data:KapstelkiData;
		private var _pnum:uint;
		
		public function KapstelikiPage__(data:KapstelkiData, pnum:uint) {
			super();
			
//			setStyle("backgroundAlpha", 1);
//			setStyle("backgroundImage", backgroundImage);
			
			this._data = data;
			this._pnum = pnum;
			addEventListener(PreloadingImageEvent.CANT_ZOOM, cantZoom);
			addEventListener(PreloadingImageEvent.CAN_ZOOM, canZoom);
			addEventListener(PreloadingImageEvent.LOAD, load);
		}
		
		override protected function createChildren():void {
			super.createChildren();
			var canvas:GradientCanvas = new GradientCanvas();
			canvas.startColor = side == Page.LEFT ? 0x0898BE : 0x46B3CF; 
			canvas.endColor = side == Page.LEFT ? 0x46B3CF : 0x0898BE;
			canvas.angle = 0;
			canvas.setStyle("horizontalCenter", 0);
			canvas.setStyle("verticalCenter", 0);
			canvas.width = this.width - 50;
			canvas.height = this.height - 50;
			addChild(canvas);
			
			loadImages();
		}

		private function loadImages():void {
			var cols:uint = _data.cols;
			var rows:uint = _data.rows;
			var imgWidth:uint = _data.imgWidth;
			var imgHeight:uint = _data.imgHeight;
			var imgInitialWidth:uint = _data.imgInitialWidth;
			var imgInitialHeight:uint = _data.imgInitialHeight;
			var verticalGap:uint = _data.verticalGap;
			var horizontalGap:uint = _data.horizontalGap;
			var kapstelki:ArrayCollection = _data.kapstelki;
						 
			var img:KapstelekImageCanvas;
			var kapstelek:KapstelekData;
			var n:uint = rows*cols*_pnum;
			
			var xCenter:uint = width/2 - (cols*imgInitialWidth + (cols-1)*horizontalGap)/2;
			var yCenter:uint = height/2 - (rows*imgInitialHeight + (rows-1)*verticalGap)/2;
			var count:uint = n;
			for (var row:uint=0; row<rows; row++) {
				for (var col:uint=0; col<cols; col++) {
					if (kapstelki.length > count) {
						kapstelek = KapstelekData(kapstelki.getItemAt(count));
						img = new KapstelekImageCanvas(imgWidth/imgInitialWidth, imgHeight/imgInitialHeight, imgInitialWidth, imgInitialHeight, kapstelek.imgSrc, kapstelek.toolTipValue);
						img.x = xCenter + horizontalGap*col + col*imgInitialWidth;
						img.y = yCenter + verticalGap*row + row*imgInitialHeight;
						addChild(img);
						count++;
					}
				}
			}						
		}
		
		private function load(event:PreloadingImageEvent):void {
			//trace("KapstelikiPage::load " + event.index);
			if (!loaded) {
				if (event.index%2 == 0) {
					var i:int = index - event.index;
					if (i == 1 || i == 2) {
						loaded = true;
						dispatch(PreloadingImageEvent.LOAD, event.index);	
					}			
				}
			}
		}
		
		private function canZoom(event:PreloadingImageEvent):void {
//			trace("kp can");
			dispatch(PreloadingImageEvent.CAN_ZOOM, event.index);
		}

		private function cantZoom(event:PreloadingImageEvent):void {
//			trace("kp cant");
			dispatch(PreloadingImageEvent.CANT_ZOOM, event.index);
		}
		
		private function dispatch(event:String, index:int):void {
			var children:Array = getChildren();
			for each (var child:DisplayObject in children) {
				if (child is KapstelekImageCanvas) {
					child.dispatchEvent(new PreloadingImageEvent(event, index));					
				}
			}
		}
	
//		public static function createKapstelkiPage():KapstelikiPage {
//			var page:KapstelikiPage = new KapstelikiPage(KapstelkiData.createKapstelkiData());
//			page.setStyle("backgroundColor", "#FFFFFF");
//			page.setStyle("backgroundAlpha", 1);
//			page.setStyle("backgroundImage", backgroundImage);

//			page.width = ;
//			page.height = ;
//			page.imgWidth = 150;
//			page.imgHeight = 150;
//			page.horizontalGap = 10;
//			page.verticalGap = 10;
//			page.widthFrom = 0.2;
//			page.heightFrom = 0.2;
//			page.widthTo = 0.4;
//			page.heightTo = 0.4;
//			return page;
//		}
	}
	
}